package xj.graph2d.shapes.block;

import xj.graph2d.DrawShape;
import xj.graph2d.DrawShapeConstants;
import xj.graph2d.TextShape;

public class BlockShapeUtil { 

  // Class/Method block

  public static void setName(BlockShape c, String name) { 
    if (c != null) { 
      DrawShape s = c.findInRole(BlockShape.NAME);
      if (s instanceof TextShape) { 
	TextShape t = (TextShape) s;
	t.setText(name);
      }
    }
  }

  public static String getName(BlockShape c) { 
    if (c != null) { 
      DrawShape s = c.findInRole(BlockShape.NAME);
      if (s instanceof TextShape) { 
	TextShape t = (TextShape) s;
	return t.getText();
      }
    }
    return null;
  }

  public static BlockShape findSubBlockOfType(BlockShape c, int type) { 
    if (c != null) { 
      for (DrawShape s : c.getShapes()) { 
	if (s instanceof BlockShape) {
	  BlockShape b = (BlockShape) s;
	  if (b.getBlockType() == type) { 
	    return b;
	  }
	}
      }
    }
    return null; 
  }

  // Class block
  public static void addAttribute(BlockShape c, 
				  SimpleStatementShape a,
				  boolean isStatic,
				  boolean isConstant) { 
    if (c != null && a != null) { 
      int btype = BlockShape.ATTRIBUTE_LIST;
      if (isConstant) { 
	btype = BlockShape.CONSTANT_LIST;
      } else if (isStatic) { 
	btype = BlockShape.STATIC_FIELD_LIST;
      }
      for (DrawShape s : c.getShapes()) { 
	if (s instanceof BlockShape) {
	  BlockShape b = (BlockShape) s;
	  if (b.getBlockType() == btype) { 
	    b.addShape(a);
	    break;
	  }
	}
      }
    }
  }
  
  public static void addMethodDecl(BlockShape c, 
				   SimpleStatementShape m, 
				   boolean ctor,
				   boolean isStatic) { 
    if (c != null && m != null) { 
      int btype = BlockShape.METHOD_LIST;
      if (ctor) {
	btype = BlockShape.CONSTRUCTOR_LIST;
      } else if (isStatic) { 
	btype = BlockShape.STATIC_METHOD_LIST;
      }
      for (DrawShape s : c.getShapes()) { 
	if (s instanceof BlockShape) {
	  BlockShape b = (BlockShape) s;
	  if (b.getBlockType() == btype) { 
	    b.addShape(m);
	    break;
	  }
	}
      }
    }
  }
  
  // Method block 
  
  public static void setType(BlockShape c, String name) { 
    if (c != null) { 
      DrawShape s = c.findInRole(BlockShape.TYPE);
      if (s instanceof TextShape) { 
	TextShape t = (TextShape) s;
	t.setText(name);
      }
    }
  }

  public static String getType(BlockShape c) { 
    if (c != null) { 
      DrawShape s = c.findInRole(BlockShape.TYPE);
      if (s instanceof TextShape) { 
	TextShape t = (TextShape) s;
	return t.getText();
      }
    }
    return null;
  }

  public static void addParameter(BlockShape m, SimpleStatementShape p) { 
    if (m != null && p != null) { 
      for (DrawShape s : m.getShapes()) { 
	if (s instanceof BlockShape) {
	  BlockShape b = (BlockShape) s;
	  if (b.getBlockType() == BlockShape.PARAMETER_LIST) { 
	    b.addShape(p);
	    break;
	  }
	}
      }
    }
  }
  
  public static void addStatement(BlockShape m, StatementShape stmt) { 
    if (m != null && stmt != null) { 
      for (DrawShape s : m.getShapes()) { 
	if (s instanceof BlockShape) {
	  BlockShape b = (BlockShape) s;
	  if (b.getBlockType() == BlockShape.METHOD_BODY) { 
	    b.addShape(stmt);
	    break;
	  }
	}
      }
    }
  }

  public static BlockShape getMethodBody(BlockShape m) { 
    return getSubBlock(m, BlockShape.METHOD_BODY);
  }
  
  public static BlockShape getSubBlock(BlockShape b, int type) { 
    if (b != null) { 
      for (DrawShape s : b.getShapes()) { 
	if (s instanceof BlockShape) {
	  BlockShape b0 = (BlockShape) s;
	  if (b0.getBlockType() == type) {
	    return b0; 
	  }
	}
      }
    }
    return null;
  }
  
  public static void removeSubBlock(BlockShape b, int type) { 
    if (b != null) { 
      for (DrawShape s : b.getShapes()) { 
	if (s instanceof BlockShape) {
	  BlockShape b0 = (BlockShape) s;
	  if (b0.getBlockType() == type) {
	    b.removeShape(s); 
	  }
	}
      }
    }
  }

  // Loop, etc

  public static void setExpressionText(BlockShape block, int role, String text) { 
    if (block != null) { 
      DrawShape s = block.findInRole(role);
      if (s != null) { 
	DrawShape s1 = s.findInRole(DrawShapeConstants.ROLE_NODE_LABEL);
	if (s1 instanceof TextShape) { 
	  TextShape tshape = (TextShape) s1; 
	  tshape.setText(text);
	}
      }
    }
  }

  public static void setExpressionText(DrawShape eshape, String text) { 
    if (eshape != null) { 
      DrawShape s1 = eshape.findInRole(DrawShapeConstants.ROLE_NODE_LABEL);
      if (s1 instanceof TextShape) { 
	TextShape tshape = (TextShape) s1; 
	tshape.setText(text);
      }
    }
  }  

}